package packet;

import java.nio.ByteBuffer;

import player.Player;

public abstract class Packet 
{  
	public Packet(int ID, int SN)
	{
		packetID = ID;
		packetSN = SN;
	}
	
	public Packet()
	{
		packetID = 0;
		packetSN = 0;
	}
	
	public int execute(Player player, int moduleFlag) 
	{ 
		return 0; 
	}
	
	public int writeToBuffer(ByteBuffer buffer)
	{
		int mark = buffer.position();
		
		buffer.putInt(packetID);
		buffer.putInt(packetSN);
		
		return (buffer.position() - mark);
	}
	
	public int readFromBuffer(ByteBuffer buffer)
	{
		int mark = buffer.position();
		
		packetID = buffer.getInt();
		packetSN = buffer.getInt();
		
		return (buffer.position() - mark);
	}
	
	public String getPacketName()
	{
		return "Packet";
	}
	
	protected abstract String getPacketDetail();
	
	public final String getPacketDesc()
	{
		String desc = String.format("%s: id=0x%08x,sn=0x%08x,", getPacketName(), getID(), getSN()); 
		
		return desc + getPacketDetail();
	}
	
	public int getPacketSize()
	{
		return 8;
	}
	
	public final int getID()
	{
		return packetID;
	}
	
	public final int getSN()
	{
		return packetSN;
	}
	
	public final void setSN(int sn)
	{
		packetSN = sn;
	}
	
	public int getCallerAllowable()
	{
		return 0;
	}
	
	public final boolean isCallerAllowable(int moduleFlag)
	{
		int callerAllowable = getCallerAllowable();
		
		return ((callerAllowable & moduleFlag) != 0);
	}
	 
	private int packetID;
	private int packetSN;
}
